#include "SD_MMC.h"
#include "Arduino.h"


const int DAT3_PIN = 2;

// 接口连接如下：
// SD卡 - ESP32
// ------------
// DAT2 -        IO
// DAT3 - SS   - IO10
// CMD  - MOSI - IO11  
// CLK  - SCK  - IO12
// DAT0 - MISO - IO13
// DAT1 -      - IO
// static const uint8_t SS    = 10;
// static const uint8_t MOSI  = 11;
// static const uint8_t MISO  = 13;
// static const uint8_t SCK   = 12;
#define MMC_CLK 12
#define MMC_CMD 11
#define MMC_DAT0 13
#define MMC_DAT3 2

#define SD_CS_PIN 10
// clk,cmd,d0
// 函数声明
bool setupSDCard();
void writeToFile(const char* filename, const char* data);
bool renameFile(const char* oldName, const char* newName);
void readFile(const char* filename);
void printSDCardInfo();

void setup() {
    Serial.begin(115200);
    Serial.println();
    pinMode(2,OUTPUT);
    digitalWrite(2,1);

    pinMode(10,OUTPUT);
    digitalWrite(10,1);
    // digitalWrite(10,HIGH);

    // while(1)
    {
        setupSDCard();
        delay(100);
    }


    const char* testFileName = "/test.txt";
    const char* retestFileName = "/retest.txt";
    const char* testData = "hello world\r\n";

    writeToFile(testFileName, testData);

    if (renameFile(testFileName, retestFileName)) {
        Serial.println("Renamed test.txt to retest.txt!");
    }

    readFile(retestFileName);
    printSDCardInfo();
}

void loop() {
    // 主循环为空，可根据需求添加其他功能
    delay(1000);
}

bool setupSDCard() {
    SD_MMC.setPins(MMC_CLK, MMC_CMD, MMC_DAT0);
    // 挂载文件系统
    if (!SD_MMC.begin("/sdcard",true)) {
        Serial.println("Failed to mount SD card");
        return false;
    }
    uint8_t cardType = SD_MMC.cardType();
    if (cardType == CARD_NONE) {
        Serial.println("No SD card connected");
        SD_MMC.end();
        return false;
    } else if (cardType == CARD_MMC) {
        Serial.println("MMC card mounted");
    } else if (cardType == CARD_SD) {
        Serial.println("SDSC card mounted");
    } else if (cardType == CARD_SDHC) {
        Serial.println("SDHC card mounted");
    } else {
        Serial.println("Unknown SD card type mounted");
    }
    return true;
}

void writeToFile(const char* filename, const char* data) {
    File file = SD_MMC.open(filename, FILE_WRITE);
    if (!file) {
        Serial.printf("Failed to open/create %s file!\n", filename);
        return;
    }
    Serial.printf("Opened/created %s file!\n", filename);
    size_t written = file.write((const uint8_t*)data, strlen(data));
    if (written != strlen(data)) {
        Serial.printf("Error writing to %s file!\n", filename);
    }
    file.close();
}

bool renameFile(const char* oldName, const char* newName) {
    if (!SD_MMC.rename(oldName, newName)) {
        Serial.printf("Failed to rename %s to %s!\n", oldName, newName);
        return false;
    }
    return true;
}

void readFile(const char* filename) {
    File file = SD_MMC.open(filename, FILE_READ);
    if (!file) {
        Serial.printf("Failed to open %s file!\n", filename);
        return;
    }
    Serial.print("File content is: ");
    while (file.available()) {
        Serial.print((char)file.read());
    }
    Serial.println();
    file.close();
}

void printSDCardInfo() {
    Serial.printf("Total SD card size: %lluMB \n", SD_MMC.cardSize() / (1024 * 1024));
    Serial.printf("Total file system size: %lluB \n", SD_MMC.totalBytes());
    Serial.printf("Used file system size: %lluB \n", SD_MMC.usedBytes());
}    